package com.example.controller;

import com.example.anno.Log;
import com.example.pojo.Clazz;
import com.example.pojo.ClazzQueryParam;
import com.example.pojo.PageResult;
import com.example.pojo.Result;
import com.example.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Slf4j
@RestController
@RequestMapping("/clazzs")
public class ClazzController {
    @Autowired
    private ClazzService clazzService;

    /**
     * 条件分页查询班级
     */
    @GetMapping
    public Result page(ClazzQueryParam clazzQueryParam){
        log.info("条件分页查询班级: {}", clazzQueryParam);
        PageResult<Clazz> page = clazzService.page(clazzQueryParam);

        return Result.success(page);
    }

    /**
     * 根据ID批量删除班级
     */
    @Log
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) throws Exception {
        log.info("根据ID删除班级: {}", id);
        clazzService.delete(id);

        return Result.success();
    }

    /**
     * 新增班级
     */
    @Log
    @PostMapping
    public Result add(@RequestBody Clazz clazz){
        log.info("新增班级: {}", clazz);
        clazzService.add(clazz);

        return Result.success();
    }

    /**
     * 根据Id查询班级
     */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id){
        log.info("根据Id查询班级: {}", id);

        return Result.success(clazzService.getById(id));
    }

    /**
     * 修改班级
     */
    @Log
    @PutMapping
    public Result update(@RequestBody Clazz clazz){
        log.info("修改班级: {}", clazz);
        clazzService.update(clazz);

        return Result.success();
    }

    /**
     * 查询所有班级
     */
    @GetMapping("/list")
    public Result getAllInfo(){
        log.info("查询所有班级");

        return Result.success(clazzService.getAllInfo());
    }
}
